#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

typedef struct {
  int id;
  char name[16];
  int roomId;
} Student;

typedef struct Node {
  Student stu;
  struct Node *next;
} Node, *LinkList;

void emptyBlankLine() {
  while (getchar() != '\n') {
    continue;
  }
}

LinkList initHead() {
  LinkList head = (LinkList)malloc(sizeof(Node));
  return head;
}

void addByHead(LinkList head) {
  Node *node = (Node *)malloc(sizeof(Node));
  puts("请输入要录取的数据(学号 姓名 房间号):");
  scanf("%d %s %d", &node->stu.id, node->stu.name, &node->stu.roomId);
  emptyBlankLine();
  node->next = head->next;
  head->next = node;
}

void createByTail(LinkList head) {
  int count, index;
  Node *current, *node;
  current = head;
  puts("程序初始化运行,请输入初始录入的人数:");
  scanf("%d", &count);
  emptyBlankLine();
  puts("请输入要录取的数据(学号 姓名 房间号):");
  for (index = 0; index < count; index++) {
    node = (Node *)malloc(sizeof(Node));
    scanf("%d %s %d", &node->stu.id, node->stu.name, &node->stu.roomId);
    emptyBlankLine();
    current->next = node;
    current = node;
  }
  current->next = NULL;
}

void showOne(const Node *current) {
  printf("学号:%4d姓名:%6s房间号:%4d\n", current->stu.id, current->stu.name,
         current->stu.roomId);
}

void showList(const LinkList head) {
  Node *current = head;
  if (head->next) {
    puts("学生信息如下:");
    while (current->next) {
      current = current->next;
      showOne(current);
    }
  } else {
    puts("暂无学生信息");
  }
}

Node *findItem(LinkList head) {
  int i = 0;
  char name[16];
  Node *current = head;
  puts("请输入要查找的学生名:");
  scanf("%s", name);
  emptyBlankLine();
  while (current->next) {
    i++;
    current = current->next;
    if (strcmp(current->stu.name, name) == 0) {
      printf("查找%d次\n", i);
      return current;
    }
  }
  return NULL;
}

void sortList(LinkList head) {
  Node *previous, *current, *temp;
  previous = head;
  current = head->next;
  while (current->next) {
    while (previous != current &&
           previous->next->stu.id < current->next->stu.id) {
      previous = previous->next;
    }
    if (previous == current) {
      current = current->next;
    } else {
      temp = current->next;
      current->next = temp->next;
      temp->next = previous->next;
      previous->next = temp;
    }
    previous = head;
  }
}

void deleteItem(LinkList head) {
  char name[16];
  Node *previous = head;
  printf("输入要删除的学生姓名:");
  scanf("%s", name);
  emptyBlankLine();
  while (previous->next && strcmp(previous->next->stu.name, name)) {
    previous = previous->next;
  }
  if (previous->next) {
    previous->next = previous->next->next;
    free(previous->next);
    puts("删除成功!");
  } else {
    puts("未找到该学生信息!");
  }
}

int main(void) {
  int choose;
  Node *current;
  LinkList head = initHead();
  createByTail(head);
  showList(head);
  while (!(choose = 0)) {
    puts("请选择您要进行的操作\n"
         "1.查看所有学生数据\t2.按名字查找学生数据\n"
         "3.按学号排序并查看数据\t4.添加学生数据\n"
         "5.删除学生数据\t6.退出");
    scanf("%d", &choose);
    emptyBlankLine();
    switch (choose) {
    case 1:
      showList(head);
      break;
    case 2:
      current = findItem(head);
      if (current) {
        showOne(current);
      } else {
        puts("找不到该学生");
      }
      break;
    case 3:
      sortList(head);
      showList(head);
      break;
    case 4:
      addByHead(head);
      showList(head);
      break;
    case 5:
      deleteItem(head);
      showList(head);
      break;
    case 6:
      exit(0);
      break;
    default:
      puts("请输入正确的指令!");
    }
  }
  return false;
}
